// pages/index/list/list.js
const app = getApp();
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    postsList: [], //总的活动
    postsShowSwiperList: [], //轮播图显示的活动
    currentPage: 0, //要跳过查询的页数
    limitPage: 3,//首先显示3条数据（之后加载时都增加3条数据，直到再次加载不够3条）
    isEmpty: false, //当前查询出来的数据是否为空
    totalCount: 0, //总活动数量
    endPage: 0, //最后一页加载多少条
    totalPage: 0, //总页数
    curIndex: 0,
    windowHeight1: 0,
    windowWidth1: 0,
    latitude: 0,
    longitude: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.showLoading({
      title: '定位中...',
    })
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy
        that.setData({
          'latitude': latitude,
          'longitude': longitude
        });
        var molist = new Array();
        wx.request({
          url: app.globalData.hostName + '/microappointment/activity/findNearby',
          method: "POST",
          data:
          {
            "latitude": that.data.latitude,
            "longitude": that.data.longitude
          },
          success: function (res) {
            wx.hideLoading();
            var results = res.data.data;
            for (var i = 0; i < results.length; i++) {
              var tempPicArray = new Array();
              if (results[i].pictureList.length > 0) {
                tempPicArray = results[i].pictureList;
              } else {
                tempPicArray = [{ pictureUrl: "https://www.daliandaxue.cn/img/not_upload.jpg" }];
              }
              var siglePic = tempPicArray[0];
              var jsonA = {
                "publisherPic": results[i].publisherPic || "/image/1.jpg",
                "publisherName": results[i].publisherName || '',
                "createTime": util.getDateDiff(results[i].createTime),
                "title": results[i].title || '',
                "actPic": siglePic.pictureUrl || '',
                "endtime": util.getDateDiff2(results[i].expireTime),
                "id": results[i].id || '',
                "publisherId": results[i].id || '',
                "address": results[i].location || ''
              }
              molist.push(jsonA);
              console.log("molist", molist);
            }
            that.setData({
              postsList: molist
            })
          },
          error: function (error) {
            console.log(error)
          }
        })
      },
      fail: function (res) {
        console.log("user reject the request" + res)
        wx.showModal({
          title: '提示',
          content: '您取消了授权，这会导致部分功能异常。如需要重新授权，请先删除本小程序后重新加载并授权。',
          success: function (res) {

          }
        })
      }
    })
    
    
  },

  //数据存储
  onSetData: function (data) {
    console.log(data.length);
    let page = this.data.currentPage + 1;
    //设置数据
    data = data || [];
    this.setData({
      postsList: page === 1 || page === undefined ? data : this.data.postsList.concat(data),
    });
    console.log(this.data.postsList, page);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


  goToDetail: function (e) {
    console.log("debug e.currentTarget.id", e.currentTarget.dataset.id);
    wx.navigateTo({
      //url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
      url: "/pages/detail/detail?id=" + e.currentTarget.dataset.id
    })
  }
})